package com.dd.cloud.user.service.aicms.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dd.cloud.user.config.ZerkProperties;
import com.dd.cloud.user.entity.aicms.AicmsDoctor;
import com.dd.cloud.user.mapper.aicms.AicmsDoctorMapper;
import com.dd.cloud.user.service.aicms.IAicmsDoctorService;
import com.dd.cloud.user.vo.DoctorVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author czg
 * @since 2022-04-27
 */
@Service
@Slf4j
public class AicmsDoctorServiceImpl extends ServiceImpl<AicmsDoctorMapper, AicmsDoctor> implements IAicmsDoctorService {
    @Autowired
    private RestTemplate restTemplate;
    @Autowired
    private ZerkProperties zerkProperties;

    /**
     * 通过疾病id查询专家信息
     * @param jibingId 疾病id
     * @return
     */
    @Override
    public AicmsDoctor queryDoctor(String jibingId) {
        try {
            String str = restTemplate.getForObject(zerkProperties.getZhuangJiaInfoUrl() + jibingId, String.class);
            DoctorVo vo = JSON.parseObject(str, DoctorVo.class);
            log.info("根据疾病获取专家信息 参数jibingid=：" + jibingId);
            log.info("根据疾病获取专家信息结果：" + vo);
            if (vo != null && vo.getnErrorCode() == 0) {
                AicmsDoctor doctor = vo.getJibing().get(0);
                doctor.setSInfo(vo.getsInfo());
                doctor.setSPhoto(zerkProperties.getDoctorImgUrl() + doctor.getSPhoto());
                return doctor;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 通过疾病序号查询专家信息
     * @param sXuHao
     * @return
     */
    @Override
    public AicmsDoctor queryDoctorByXuhao(String sXuHao) {
        QueryWrapper<AicmsDoctor> qw = new QueryWrapper<>();
        //qw.lambda().select(AicmsDoctor::getNID);
        qw.lambda().eq(AicmsDoctor::getSXuHao, sXuHao);
        qw.last("limit 1");
        AicmsDoctor aicmsDoctor = getOne(qw);
        log.info("根据疾病序号查询专家信息:{}", aicmsDoctor);
        return aicmsDoctor;
    }
}
